//=======================配置模块=======================
Config={
		host: "http://cijqt8.natappfree.cc"
}
//======================================================

// 页面刷新
$(document).on("click", "#f2-fresh", function() {
	document.location.reload()
})


// =======================历史模块=========================
function HistoryModule() {
	// 历史列表table
	var hisList = $("#hisList")
	var mList = [] // 历史列表数据

	// 加载设备上的模型列表
	function loadModeList() {
		$.get(Config.host + "/list", function(result) {
			hisList.html('')
			mList = result
			$.each(mList, function(idx, o) {
				var row = '<tr><td>' + o.name + '</td>' + '<td><img src="' + Config.host + o.image + '" style="height: 5em;" alt=""></td>' + '<td><button idx="' + idx
						+ '" class="btn btn-primary  f2-print">开始打印</button>' + '	 <button idx="' + idx + '" class="btn btn-primary  f2-delete">删除</button>' + '</td></tr>'
				hisList.append(row)
			});
		});
	}

	// 历史记录列表开始打印事件
	$(document).on("click", ".f2-print", function() {
		alert(mList[$(this).attr("idx")].name);
	})
	// 删除模型事件
	$(document).on("click", ".f2-delete", function() {
		if (confirm("确定要删除吗?")) {
			var index = $(this).attr("idx")
			var name = mList[index].name;
			$.get(Config.host + "/remove?name=" + name, function(data, status, xhr) {
				if (ok(xhr)) {
					Toast("删除" + name + "成功")
					$('#hisList tr:eq(' + index + ')').remove();
				}
			});
		}
	})
	return {
		loadModeList : loadModeList,
	}
}

// ==========================================================

// =======================监控模块 ==========================
function MonitorModule() {
	var txtLastTime = $("#f2-layer-last-time")
	var processBar = $("#print-percent")
	var stateTxt = $("#state_txt")
	
	var layerTime = 60
	var printPercnet = 70

	$(document).on("click", "#f2-start", function() {
		Toast("开始打印")
	})
	$(document).on("click", "#f2-stop", function() {
		Toast("暂停打印")
	})
	$(document).on("click", "#f2-continue", function() {
		Toast("继续打印")
	})

	function setProgress(w) {
		processBar.width(w + '%');
		processBar.text(w + '%');
	}

	function startMonitor() {
		// var intervalLayer = setInterval(function() {
		
		//当前层固化倒计时
		if (layerTime < 1)
			layerTime = 60
		layerTime = layerTime - 1
		txtLastTime.text(layerTime + 's')

		//打印进度
		if (printPercnet > 100)
			printPercnet = 0
		setProgress(printPercnet)
		printPercnet = printPercnet + 1
		
		//打印实时信息
		$.get("json/state.json", function(result) {
			stateTxt.html(uuid());
		});
		// }, 1000);
	}
	function stopMonitor() {

	}
	return {
		startMonitor : startMonitor,
		stopMonitor : stopMonitor
	}
}
// ============================================================

// ===========================Main=============================

var monitorM = MonitorModule()
var historyM = HistoryModule()

monitorM.startMonitor()
historyM.loadModeList()

// =============================================================

// =======================共用函数==============================
/**
 * 自定义Toast弹窗
 * 
 * @param msg
 * @param duration
 * @returns
 */
function Toast(msg, duration) {
	duration = isNaN(duration) ? 2000 : duration;
	var m = document.createElement('div');
	m.innerHTML = msg;
	m.style.cssText = "font-family:siyuan;max-width:60%;min-width: 150px;padding:0 14px;height: 40px;color: rgb(255, 255, 255);line-height: 40px;text-align: center;border-radius: 4px;position: fixed;top: 50%;left: 50%;transform: translate(-50%, -50%);z-index: 999999;background: rgba(0, 0, 0,.7);font-size: 16px;";
	document.body.appendChild(m);
	setTimeout(function() {
		var d = 0.5;
		m.style.webkitTransition = '-webkit-transform ' + d + 's ease-in, opacity ' + d + 's ease-in';
		m.style.opacity = '0';
		setTimeout(function() {
			document.body.removeChild(m)
		}, d * 1000);
	}, duration);
}

function uuid() {
	var s = [];
	var hexDigits = "0123456789abcdef";
	for (var i = 0; i < 36; i++) {
		s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
	}
	s[14] = "4";
	s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);
	s[8] = s[13] = s[18] = s[23] = "-";
	var uuid = s.join("");
	return uuid;
}

function ok(xhr) {
	if (xhr.status == 200)
		return true
	return false
}

// ================================================================
